package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Activity;
import com.gaohe.service.ActivityService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    ActivityService activityService;

    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/page")
    public R page(@RequestParam Integer page,@RequestParam Integer pageSize, @RequestParam(required = false) String name)
    {
        Page<Activity>pages = new Page<>(page, pageSize);
        LambdaQueryWrapper<Activity>lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(!StringUtils.isEmpty(name),Activity::getActivityName,name);
        System.out.println(pages);
        return R.success(activityService.page(pages,lambdaQueryWrapper));
    }
    @GetMapping("/getActivityList")
    public R getList()
    {
        List<Activity> list = activityService.list();
        return R.success(list);
    }
//    添加
    @PostMapping
    public R save(@RequestBody Activity activity,HttpSession session)
    {
        activity.setStatus(1);
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
//        Long user = (Long) session.getAttribute("user");
        Long user = (Long)redisTemplate.opsForValue().get("auth");
        activity.setCreateUser(user);
        activity.setUpdateUser(user);
        boolean flag = activityService.save(activity);
        return flag?R.success("添加成功"):R.error("添加失败");

    }
    //    获取id
    @GetMapping("{id}")
    private R getId(@PathVariable Long id)
    {
        return R.success(activityService.getById(id));
    }

//    更新
    @PutMapping
    public R update(@RequestBody Activity activity)
    {
        activity.setUpdateTime(LocalDateTime.now());
        boolean flag = activityService.updateById(activity);
        return flag?R.success("更改成功"):R.error("更改失败");
    }

//    删除
    @DeleteMapping
    public R deleted(Long id)
    {
        Activity activity = activityService.getById(id);
        activity.setUpdateTime(LocalDateTime.now());
        boolean flag = activityService.removeById(activity);
        return flag?R.success("删除成功"):R.error("删除失败");
    }
//    修改状态
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody Activity activity, HttpSession httpSession)
    {
//        activity.setStatus(0);
        activity.setUpdateTime(LocalDateTime.now());
//        (long)httpSession.getAttribute("username")
        activity.setUpdateUser((Long)redisTemplate.opsForValue().get("auth"));
        boolean flag = activityService.updateById(activity);
        return flag?R.success("修改成功"):R.error("修改失败");
    }
}
